//
//  OrderDetailsViewController.m
//  CenturyInterconnection
//
//  Created by l-sheng on 6/18/16.
//  Copyright © 2016 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "OrderDetailsViewController.h"
#import "OrderDetailTableViewCell.h"
#import "OrderHistoryModel.h"

@interface OrderDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,retain)UITableView * tableView;
@property (nonatomic,strong) NSDictionary * allData;
@property(nonatomic,retain)NSMutableArray * modelArray;

@end

@implementation OrderDetailsViewController



- (instancetype)init{
    self = [super init];
    if(self){
    }
    return self;
}


- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _allData = [[NSDictionary alloc]init];
    _modelArray = [[NSMutableArray alloc]init];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self makeNavBtn];
    [self creatTableView];

    [self getData];
    
    
}

-(void)makeNavBtn{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:0];
    leftButton.frame=CGRectMake(0, 0, ACTUAL_WIDTH(23), ACTUAL_HEIGHT(23));
    [leftButton addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*item= [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem=item;
}
-(void)comeBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getData{
    
    // http://viprunningonline.com/api/index.php?main_page=account_history_info
    
    NSString * urlStrr=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
    
    NSDictionary * paramss=@{   @"main_page":@"account_history_info",
                                @"zenid":[UserSession instance].cookie,
                                @"order_id":self.orderID
                            };
  
    HttpManager*managerr=[[HttpManager alloc] init];
    
    [managerr getDataFromNetworkWithUrl:urlStrr parameters:paramss compliation:^(id data, NSError *error) {
       

        NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
        
        if ([number isEqualToString:@"1"]) {
            
            self.allData = data[@"data"];
            
            NSArray * productsArr = data[@"data"][@"products"];
            for (NSDictionary * dict in productsArr) {
                OrderHistoryModel * model = [[OrderHistoryModel alloc]initWithDictionary:dict];
                [_modelArray addObject:model];
            }
            
            
            [_tableView reloadData];
        }else{
            [JRToast showWithText:data[@"status_message"] duration:2.0];
        }

    }];
}


-(void)creatTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    [_tableView registerClass:[OrderDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.offset(0);
        make.right.offset(0);
        make.bottom.offset(0);
    }];
    
}


#pragma mark --- tableViewDelegateMethod
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _modelArray.count;
    }else{
        return 0;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    OrderHistoryModel * model = _modelArray[indexPath.row];
    cell.model = model;
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section ==0) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight*0.05)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, KScreenWidth-30, KScreenHeight*0.05)];
        label.text = [NSString stringWithFormat:@"Order Number:%@",_orderID];
        label.textAlignment = NSTextAlignmentLeft;
        if (iPhone5) {
            label.font = [UIFont systemFontOfSize:14];
        }else{
            label.font = [UIFont systemFontOfSize:15];
        }
        [view addSubview:label];
        return view;
    }else if(section == 1){
        CGFloat height = [self getSecondSectionHeight];
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, height)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel * deliveryLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 3, KScreenWidth-30, height-6)];
        if (iPhone5) {
            deliveryLabel.font = [UIFont systemFontOfSize:14];
        }else{
            deliveryLabel.font = [UIFont systemFontOfSize:15];
        }
        deliveryLabel.numberOfLines = 0;
        NSDictionary * dict = _allData[@"delivery"];
        deliveryLabel.text = [NSString stringWithFormat:@"Address:%@,%@ %@,%@,%@,%@",dict[@"street_address"],dict[@"city"],dict[@"postcode"],dict[@"state"],dict[@"country"],dict[@"telephone"]];
        [view addSubview:deliveryLabel];
        return view;
        
    }else{
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight*0.1)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel * priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 3, KScreenWidth-30, KScreenHeight*0.05-6)];
        priceLabel.textAlignment = NSTextAlignmentRight;
        priceLabel.text = [NSString stringWithFormat:@"Total Price:%@",_allData[@"info"][@"total"]];
        if (iPhone5) {
            priceLabel.font = [UIFont systemFontOfSize:14];
        }else{
            priceLabel.font = [UIFont systemFontOfSize:15];
        }
        [view addSubview:priceLabel];
        
        UILabel * statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, KScreenHeight*0.05+3, KScreenWidth-30, KScreenHeight*0.05-6)];
        statusLabel.textAlignment = NSTextAlignmentRight;
        statusLabel.text = [NSString stringWithFormat:@"Order Status:%@",_allData[@"info"][@"orders_status"]];
        if (iPhone5) {
            statusLabel.font = [UIFont systemFontOfSize:14];
        }else{
            statusLabel.font = [UIFont systemFontOfSize:15];
        }
        [view addSubview:statusLabel];
        
        return view;
    }
    
}
-(CGFloat)getSecondSectionHeight{
    NSDictionary * dict = _allData[@"delivery"];
    NSString * str = [NSString stringWithFormat:@"Address:%@,%@ %@,%@,%@,%@",dict[@"street_address"],dict[@"city"],dict[@"postcode"],dict[@"state"],dict[@"country"],dict[@"telephone"]];
    UIFont * font;
    if (iPhone5) {
        font = [UIFont systemFontOfSize:14];
    }else{
        font = [UIFont systemFontOfSize:15];
    }
    CGRect rect = [str boundingRectWithSize:CGSizeMake(KScreenWidth-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    CGFloat height = rect.size.height + 6;
    return height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KScreenHeight*0.15;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return KScreenHeight*0.05;
    }else if(section == 1){
        return [self getSecondSectionHeight];
    }else{
        return KScreenHeight*0.1;
    }
}
@end
